import { uJob, for_obj, obj_str, uDb, cline, d_sort } from 'sui';

const load_task = (hDone, setTask) => {
    uDb(hDone, "file/list/gorder",
        {
            line: cline(),
        },
        r => {
            r.forEach(d => {
                if (d.v) {
                    d["num"] = d.v.length;
                }
            });

            d_sort(r, "sn", true);

            setTask(r);
        },
        e => setTask(null));
};

const load_box = (hDone, cur, setOut) => {
    const task_sn = obj_str(cur, "sn");

    uJob(hDone, "goods/loadpsn",
        {
            line: cline(),
            k: task_sn,
        },
        r => {
            const box_out = {};

            r.forEach(d => {
                const boxout = obj_str(d, "boxgood");

                if (!boxout) {
                    return;
                }

                if (box_out[boxout]) {
                    box_out[boxout].push(d);
                } else {
                    box_out[boxout] = [d];
                }
            });

            const ret_out = [];

            for_obj(box_out, (k, v) => {
                ret_out.push({sn: k, box: v});
            });

            setOut(ret_out);
        },
        e => setOut(null));
};

const load_pos = (box_sn, setMain) => {
    if (!box_sn) {
        setMain(null);
        return;
    }

    uDb(null, `file/read/gbox_${box_sn}`, { line: cline() },
        r => setMain(r),
        e => setMain(null));
};

export {load_pos, load_task, load_box};
